﻿using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using System;

public class WebFormController : Controller { }

public static class WebFormBrdige
{
    public static void RenderPartial(string partialName)
    {
        RenderPartial(partialName, null);
    }

    public static void RenderPartial(string partialName, object model)
    {
        var httpCtx = new HttpContextWrapper(System.Web.HttpContext.Current);
        
        var rt = new RouteData();
        rt.Values.Add("controller", "WebFormController");
        
        var ctx = new ControllerContext(new RequestContext(httpCtx, rt), new WebFormController());
        
        var view = ViewEngines.Engines.FindPartialView(ctx, partialName).View;
        
        var vctx = new ViewContext(ctx, view,
            new ViewDataDictionary { Model = model },
            new TempDataDictionary());

        view.Render(vctx, System.Web.HttpContext.Current.Response.Output);
    }

	public static string ActionLink(string linkText, string url)
	{
		return string.Format("<a href=\"{0}\">{1}</a>", url, linkText);
	}

    public static string ActionLink(string linkText, string actionName, string controllerName)
    {
		return string.Format("<a href=\"/{0}/{1}\">{2}</a>", controllerName, actionName, linkText);
    }

	public static void MVCTransfer(string path, HttpRequest request)
	{
		path = HttpUtility.UrlDecode(path);
		HttpContext.Current.RewritePath(path, false);
		IHttpHandler httpHandler = new MvcHttpHandler();
		httpHandler.ProcessRequest(HttpContext.Current);
	}
}